package com.wtgroup.demo.面试题;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/3/10 14:09
 */
public class Q003_依次打印 {
    /**
     * 两个线程, 依次打印
     */

    public static void main(String[] args) {
        char a[] = "123456789".toCharArray();
        char b[] = "abcdefghi".toCharArray();

        Object lock = new Object();
        new Thread(() -> {
            synchronized (lock) {
                for (char c : a) {
                    System.out.println(c);
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                // ! 循环结束后, 一定要 notify !
                // 如果本数组先完事, 不 notify 的话, t2 永远 wait 了
                lock.notify();
            }


        }).start();

        new Thread(() -> {
            synchronized (lock) {
                for (char c : b) {
                    System.out.println(c);
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                lock.notify();
            }

        }).start();

        // SleepUtil.sleep(100);
        System.out.println("!!!!!!!!!!!!!!!!");
    }


}
